package com.zhouchao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhouchao.bean.Goods;


public class GoodsDao {
	/**
	 * @param con
	 * @return
	 * @throws Exception
	 */
	public List<Goods> infoList(Connection con)throws Exception {
		List<Goods> infolist = new ArrayList<Goods>();
		String sql = "select * from t_goods order by id desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Goods goods=new Goods();
			goods.setDormid(rs.getString("dormid"));
			goods.setDorm(rs.getString("dorm"));
			goods.setName(rs.getString("name"));
			goods.setStunum(rs.getString("stunum"));
			goods.setDate(rs.getString("date"));
			goods.setGoods(rs.getString("goods"));
			goods.setTemp(rs.getString("temp"));
			infolist.add(goods);
		}
		return infolist;
	}
	public Goods infoList(Connection con,String id,String dorm)throws Exception {
		Goods goods=new Goods();
		String sql = "select * from t_goods where dormid='"+id+"' dorm='"+dorm+"'";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			
			goods.setDormid(rs.getString("dormid"));
			goods.setDorm(rs.getString("dorm"));
			goods.setName(rs.getString("name"));
			goods.setStunum(rs.getString("stunum"));
			goods.setDate(rs.getString("date"));
			goods.setGoods(rs.getString("goods"));
			goods.setTemp(rs.getString("temp"));
			
			System.out.println(goods);
		}
		return goods;
	}
	//添加消息
	public int intoInfo(Connection con,Goods goods) throws Exception{
		String sql = "insert into t_goods values(null,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, goods.getDormid());
		pstmt.setString(2, goods.getDorm());
		pstmt.setString(3, goods.getName());
		pstmt.setString(4, goods.getStunum());
		pstmt.setString(5, goods.getGoods());
		pstmt.setString(6, goods.getDate());
		pstmt.setString(7, goods.getTemp());
		System.out.println(sql);
		return pstmt.executeUpdate();
	}
	
	
}
